/* gcc -O2 parse.c -o parse.x -lm */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define max(A,B) ((A) > (B)) ? (A) : (B)
#define MAXBINS 200


void initparam(int *nvmin,int *nvmax,int *minseed,int *maxseed,float *taumin,float *taumax,float *dtau){
    FILE *fp=fopen("parameters","r");
    fscanf(fp,"%d\t%d\t%d\t%d\t%f\t%f\t%f",nvmin,nvmax,minseed,maxseed,taumin,taumax,dtau);
    fclose(fp);
}


/*
 inputs an integer n and converts it into a sequence of characters in *str,
 starting at str[i] and returning the integer i' where "n" ends in *str
 (str[i]='\0').
 */
int inttostring(int n,int i,char *str){
    if(n < 0){
        str[i++] = '-';
        i = inttostring(-n,i,str);
    }else{
        if((n/10) > 0) i = inttostring(n/10,i,str);
        str[i++] = '0' + n%10;
        str[i] = '\0';
    }
    return(i);
}

FILE *opennewfile(int nv,float tau,int seed){
    int k,i=0;
    FILE *fp;
    char fname[100];
    char s;
    int runpow;
    float runfac;
    
    i = inttostring(nv,i,fname);
    fname[i++] = '/';
    fname[i++] = 'V';
    fname[i++] = 'E';
    fname[i++] = 'O';
    fname[i++] = 'g';
    fname[i++] = 'p';
    fname[i++] = 'u';
    fname[i++] = '1';
    fname[i++] = '3';
    fname[i++] = 'h';
    fname[i++] = 'i';
    fname[i++] = 's';
    fname[i++] = 't';
    fname[i++] = 'o';
    fname[i++] = '_';
    i = inttostring((int)(100*tau+0.01),i,fname);
    fname[i++] = '_';
    i = inttostring(seed,i,fname);
    fp = fopen(fname,"r");
/*
    if(fp != NULL){
        k = 2;
        do{
            fscanf(fp,"%c",&s);
            k -= (s == '\n');
        } while( k );
        //            printf("#  %s %c\n",fname,s);
    }
*/
    return(fp);
}

FILE *openoutfile(int nv){
    int k,i=0;
    FILE *fp;
    char fname[100];
    
    i = inttostring(nv,i,fname);
    fname[i++] = '/';
    fname[i++] = 'h';
    fname[i++] = 'i';
    fname[i++] = 's';
    fname[i++] = 't';
    fname[i++] = 'o';
    fname[i++] = '3';
    fname[i++] = 'S';
    fname[i++] = 'p';
    fname[i++] = 'i';
    fname[i++] = 'n';
    fname[i++] = '\0';
    fp = fopen(fname,"w");
    return(fp);
}



int main(void){
    FILE *fp,*fp_out;
    int nv,nnv,nvmin,nvmax;
    int i,inst,minseed,maxseed,seed1;
    int localh,count,totalcount;
    float taumin,taumax,dtau,tau,mintime;
    int distr[MAXBINS],norm[MAXBINS],nvflag;
    
    initparam(&nvmin,&nvmax,&minseed,&maxseed,&taumin,&taumax,&dtau);
    for(nv=16; nv< 259; nv*=2){
//    for(nv=nvmax; nv>=nvmin; nv--){
        nvflag = 0;
        for(i=0; i<MAXBINS; i++) distr[i]=0;
        for(i=0; i<MAXBINS; i++) norm[i]=0;
        for(tau=taumin; tau<=taumax; tau+=dtau){
            for(seed1=minseed; seed1<=maxseed; seed1++){
                fp = opennewfile(nv,tau,seed1);
                if(fp != NULL){
                    nvflag = 1;
                    //             60            1171         98       3071
                    while(fscanf(fp,"%d%d%d%d",&localh,&count,&nnv,&totalcount) != EOF){
                        i = (localh+1)/16;
//                        i = (localh+1)/(nv/2);
//                        i = (int)(5.0*localh/nnv);
printf("%d %d %d %d %d\n",i,localh,count,nnv,totalcount);
                        if(i<MAXBINS){
                            distr[i] += count;
                            norm[i] += totalcount;
                        }
                    }
                    fclose(fp);
                }//if fp
            }//for seed
        }// for tau
        if(nvflag){
            fp_out = openoutfile(nv);
            for(i=0; i<MAXBINS; i++){
                if(norm[i])fprintf(fp_out,"%20.10e   %20.10e   %10d  %10d \n",i*16.0/nv,0.125*distr[i]/norm[i],norm[i],nv);
//                if(norm[i])fprintf(fp_out,"%20.10e   %20.10e   %10d  %10d \n",i*0.5,0.125*distr[i]/norm[i],norm[i],nv);
            }//if MAXBINS
            fclose(fp_out);
        }// if nvflag
    }//for nv
    return(0);
}

/********** END of MAIN           *** *****************************/
